package com.sxbbc.common.core.service.system.pay;

import java.math.BigDecimal;
import java.util.List;

import com.sxbbc.common.core.service.BBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.BExpConfig;
import com.sxbbc.common.core.entity.jz.BExpDetail;
import com.sxbbc.common.core.entity.jz.CReceivedAddr;
import com.sxbbc.common.core.enums.ValuationTypeEnums;
import com.sxbbc.common.core.service.IBExpConfigService;
import com.sxbbc.common.core.service.IBExpDetailService;

/**
 * 计算运费
 *
 * @author arno
 */
@Service
public class ExpService {

    @Autowired
    private IBExpConfigService ibExpConfigService;
    @Autowired
    private IBExpDetailService ibExpDetailService;
    @Autowired
    private BBusinessService bBusinessService;

    /**
     * 计算商品运费 物流提鸡
     *
     * @param receivedAddr
     * @return
     */
    public BigDecimal getExpByFarm(CReceivedAddr receivedAddr) {
        BigDecimal exp = BigDecimal.ZERO;
        //计算商品运费
        BExpConfig bExpConfig = ibExpConfigService.selectChickenExpConfig();
        if (bExpConfig == null) {
            return new BigDecimal(-1);
        }
        exp = this.getExp(receivedAddr, bExpConfig.getId(), BigDecimal.ZERO, 1, BigDecimal.ZERO, ValuationTypeEnums.AMOUNT.getId());
        return exp;
    }

    /**
     * 计算商品运费  判断商品是按数量计价还是总量计价
     *
     * @param addr          收货地址
     * @param expId         运费模板
     * @param goodMoney     货物金额
     * @param qty           数量
     * @param weight        1数量=多少重量
     * @param valuationType 商品计费类型
     * @return
     */
    public BigDecimal getExp(CReceivedAddr addr, Integer expId, BigDecimal goodMoney, Integer qty, BigDecimal weight, Integer valuationType) {
        BigDecimal expMoney = BigDecimal.ZERO;
        //如果当前商品不包含运费
        if (addr == null || expId == null || expId <= 0) {
            return expMoney;
        }
        if (addr.getCityId() == null) {
            throw new ThrowJsonException("请选择完整收货地址");
        }
        //获取运费模板信息
        BExpConfig bExpConfig = ibExpConfigService.getById(expId);
        if (bExpConfig == null || !CalcuUtils.getInstance().isEquals(bExpConfig.getStatus(), StaticUtils.STATUS_YES)) {
            return expMoney;
        }
        //计算当前数量
        BigDecimal vals = new BigDecimal(qty);
        //如果当前商量按照重量计算 则判断当前购买数量的重量
        if (CalcuUtils.getInstance().isEquals(ValuationTypeEnums.WEIGHT.getId(), valuationType)) {
            vals = weight;
        }
        //根据收货人获取到当前地址  如果当前城市没有对应规则获取默认规则
        BExpDetail bExpDetail = new BExpDetail();
        bExpDetail.setExpid(expId);
        bExpDetail.setCityid(String.valueOf(addr.getCityId()));
        bExpDetail = ibExpDetailService.getByCityid(bExpDetail);

        if (bExpDetail == null) {
            //获取默认运费
            QueryWrapper<BExpDetail> wrapper = new QueryWrapper<BExpDetail>();
            wrapper.eq("expid", expId);
            wrapper.eq("default_flag", StaticUtils.STATUS_YES);
            bExpDetail = ibExpDetailService.getOne(wrapper);
        }
        //如果当前商品存在满额免邮
        if (CalcuUtils.getInstance().isEquals(StaticUtils.STATUS_YES, bExpDetail.getType())) {
            if (bExpConfig.getMoney().compareTo(BigDecimal.ZERO) > 0) {
                if (goodMoney.compareTo(bExpConfig.getMoney()) >= 0) {
                    return expMoney;
                }
            }
            //如果当前规则 满足数量满额
            if (bExpConfig.getVals().compareTo(BigDecimal.ZERO) > 0) {
                if (vals.compareTo(bExpConfig.getVals()) >= 0) {
                    return expMoney;
                }
            }
        }
        if (bExpDetail != null) {
            if (vals.compareTo(bExpDetail.getBeginNum()) <= 0) {
                expMoney = bExpDetail.getBeginMoney();
            } else {
                expMoney = bExpDetail.getBeginMoney();
                //计算多出来的 数量是 多少倍数
                BigDecimal sub = CalcuUtils.getInstance().sub(vals, bExpDetail.getBeginNum(), 2);
                int boolVals = CalcuUtils.getInstance().mul(sub, new BigDecimal(100), 0).intValue();
                int boolAdd = CalcuUtils.getInstance().mul(bExpDetail.getAddNum(), new BigDecimal(100), 0).intValue();
                int num = 1;
                if (boolAdd > 0) {
                    if (boolVals % boolAdd == 0) {
                        num = boolVals / boolAdd;
                    } else {
                        num = boolVals / boolAdd + 1;
                    }
                }
                BigDecimal addMoney = CalcuUtils.getInstance().mul(new BigDecimal(num), bExpDetail.getAddMoney(), 2);
                expMoney = CalcuUtils.getInstance().add(expMoney, addMoney, 2);
            }
        }
        return expMoney.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : expMoney;
    }

    /**
     * 获取最大运费
     *
     * @param list
     * @return
     */
    public BigDecimal getMaxExp(List<BigDecimal> list) {
        BigDecimal exp = list.stream().reduce(BigDecimal::max).get();
        return exp;
    }
}
